package com.rpc.center.server;

import com.rpc.center.entity.ConsumerNode;
import com.rpc.center.entity.PublisherNode;
import com.rpc.center.manager.RPCServiceManager;
import com.rpc.common.entity.*;
import com.rpc.common.entity.request.BaseDTO;
import com.rpc.common.entity.request.DiscoverDTO;
import com.rpc.common.entity.request.PackageType;
import com.rpc.common.entity.request.PublishDTO;
import com.rpc.common.entity.response.DiscoverRS;
import com.rpc.common.entity.response.PublishRS;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

/**
 * 注册处理器
 */
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class CenterSocketHandler implements Runnable {
    private Socket socket;
    private RPCServiceManager serviceManager=RPCServiceManager.getServiceManager();
    public void setSocket(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run()  {
        try {
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            Object response = null;
            BaseDTO baseDTO = (BaseDTO) inputStream.readObject();
            PackageType rqType = baseDTO.getPackageType();
            System.out.println("请求类型：" + rqType);
            System.out.println(baseDTO);
            if (rqType == PackageType.PUBLISH_PACKAGE) {    //服务注册
                PublishDTO publishDTO = (PublishDTO) baseDTO;
                String serviceID = publishDTO.getInterfaceName() + publishDTO.getVersion();
                String address = publishDTO.getHostAddress();
                int port = publishDTO.getPort();
                PublisherNode publisherNode=new PublisherNode();
                publisherNode.setAddress(address);
                publisherNode.setLastKeepLiveTime(System.currentTimeMillis());
                publisherNode.setOpenHttp(publishDTO.isOpenHttp());
                serviceManager.regist(serviceID, publisherNode);//服务注册
                PublishRS publishRS = new PublishRS();
                publishRS.setSuccess(true);
                response = publishRS;
            } else if(rqType==PackageType.DISCOVER_PACKAGE){    //服务发现
                DiscoverDTO discoverDTO = (DiscoverDTO) baseDTO;
                String serviceID = discoverDTO.getInterfaceName() + discoverDTO.getVersion();
                DiscoverRS discoverRS = new DiscoverRS();
                discoverRS.setSuccess(true);
                List<PublisherNode> result=serviceManager.searchService(serviceID);  //服务查找
                System.out.println("查找到"+result.size()+"条数据");
                List<HostInfoPair> hostInfoPairs=new ArrayList<>();
                for(PublisherNode node:result){
                    hostInfoPairs.add(new HostInfoPair(node.getAddress(),node.getPort()));
                }
                discoverRS.setServerAddressList(hostInfoPairs);
                response = discoverRS;
            }else if(rqType==PackageType.HEARTBEAT_PACKAGE){

            }
            //写回响应
            System.out.println("写会响应");
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            outputStream.writeObject(response);
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
